package de.tabuma.pm.externalproject.view;

import de.tabuma.lib.tabumaview;
import de.tabuma.pm.*;
import de.tabuma.pm.externalproject.entity.*;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author alefen
 */
public class list extends tabumaview {

    public list(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        String externProjectName = "Name";
        String externProjectLoginUrl = "Login Url";
        String externProjectLoginParameter = "Login Parameter";
        String noEntry = "no entry";
        String addExternalProject = "add externes Project";
        String remove = "remove";
        
        out.println("<table class=\"contenttable\">");
        out.println("<tr>");
        out.println("<th class=\"contentth\">"+externProjectName+"</th>");
        out.println("<th class=\"contentth\">"+externProjectLoginUrl+"</th>");
        out.println("<th class=\"contentth\">"+externProjectLoginParameter+"</th>");
        out.println("<th class=\"contentth\"></th>");
        out.println("</tr>");

        List<ExternalProject> epList = (List<ExternalProject>) request.getAttribute(constants.ExternalProjectList);
        Hashtable<String, List<Parameter>> paramList = (Hashtable<String, List<Parameter>>) request.getAttribute(constants.ParameterList);

        for(int i=0;i<epList.size();i++) {

            ExternalProject ep = epList.get(i);
            String removeLink = this.getServlet()+"?"+this.getLinkAsAttribute()+"&amp;"+constants.externalProjectId+"="+ep.getId()+"&amp;"+constants.removeExternalProject+"=true";
            String updateLink = this.getServlet()+"?"+this.getLinkAsAttribute()+"&amp;"+constants.externalProjectId+"="+ep.getId()+"&amp;"+constants.updateExternalProject+"=true";

            out.println("<tr>");
            out.println("<td class=\"contenttd\"><a href=\""+updateLink+"\">"+ep.getName()+"</a></td>");
            out.println("<td class=\"contenttd\">"+ep.getLoginUrl()+"</td>");
            List<Parameter> loginParameter = paramList.get(""+ep.getId());
            out.println("<td class=\"contenttd\">");
            for(int j=0;j<loginParameter.size();j++) {
                if(j!=0) {
                    out.println(", ");
                }
                out.println(loginParameter.get(j).getShowField());
            }
            out.println("</td>");
            out.println("<td class=\"contenttd\"><a href=\""+removeLink+"\">"+remove+"</a></td>");
            out.println("</tr>");

        }

        if(epList.size()==0) {

            out.println("<tr>");
            out.println("<td class=\"contenttd\" colspan=\"4\">"+noEntry+"</td>");
            out.println("</tr>");

        }

        out.println("</table>");

        out.println(this.getFormBeginn(null));
        out.println(this.getHiddenAsAttribute());
        out.println("<input type=\"hidden\" name=\""+constants.addExternalProject+"\" value=\"true\"/>");
        out.println("<input type=\"submit\" name=\"add\" value=\""+addExternalProject+"\" class=\"button\"/>");
        out.println(this.getFormEnd());
    }
}
